package com.qeado.admin.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.qeado.admin.common.BaseConstData;
import com.qeado.admin.model.AdminAccount;
import com.qeado.admin.service.AdminAccountManageService;
import com.qeado.common.helper.AttributeHelper;

@Controller
public class AdminAccountManageController {
	@Autowired
	private AdminAccountManageService adminAccountManageService;

	@RequestMapping("/adminAccountManage.ac")
    public ModelAndView adminAccountManageIndex(HttpServletRequest request) {
		AttributeHelper.removeSessionAttribute(request, "T_");
        request.getSession().setAttribute("T_roleList", adminAccountManageService.searchRoleList());
        return new ModelAndView("admin/admin_account_manage");
    }
	
	@RequestMapping("/adminAccountBack.ac")
    public String adminAccountBack() {
        return "admin/admin_account_manage";
    }

	@RequestMapping("/adminAccountSearch.ac")
    public String adminAccountSearch(HttpServletRequest request, AdminAccount adminAccount) {
        List<AdminAccount> accountList = adminAccountManageService.searchAdminAccount(adminAccount);
        request.setAttribute("accountList", accountList);
        return "admin/admin_account_manage";
    }

	@RequestMapping("/adminAccountNew.ac")
    public String adminAccountNew(HttpServletRequest request, String id, String flag) {
        if(flag != null) {//flag==1 say that the asked from index/index.jsp
        	if(id == null || !id.equals(request.getSession().getAttribute(BaseConstData.USER_ID))) {
        		return "redirect:login.html";
        	}
        	request.setAttribute("flag", flag);
        }
        AdminAccount adminAccount = null;
        if(id != null)
        	adminAccount = adminAccountManageService.searchAdminAccountById(id);
        request.setAttribute("adminAccount", adminAccount);
        return "admin/admin_account_info";
    }

	@RequestMapping("/deleteAdminAccount.ac")
    public ModelAndView deleteAdminAccount(HttpServletRequest request, String id, AdminAccount adminAccount) {
    	adminAccountManageService.deleteAdminAccount(id);
        List<AdminAccount> accountList = adminAccountManageService.searchAdminAccount(adminAccount);
        return new ModelAndView("admin/admin_account_manage", "accountList", accountList);
    }

	@RequestMapping("/adminAccountSave.ac")
    public String adminAccountSave(HttpServletRequest request, AdminAccount adminAccount, String flag) {
		adminAccount.setCreator((String)request.getSession().getAttribute(BaseConstData.USER_ID));
    	String message = adminAccountManageService.saveAdminAccount(adminAccount);
        request.setAttribute("roleList", adminAccountManageService.searchRoleList());
        request.setAttribute("message", message);
        if(flag != null) {//flag==1 say that the asked from index/index.jsp
        	request.setAttribute("flag", flag);
        }
        return "admin/admin_account_info";
    }
    
}
